//
//  QuestionContentView.m
//  DoingPaper_LS
//
//  Created by zhuxuhong on 2016/11/4.
//  Copyright © 2016年 zhuxuhong. All rights reserved.
//

#import "QuestionContentView.h"
#import <Masonry/Masonry.h>

@interface QuestionContentView()<UIWebViewDelegate>

@property(nonatomic,strong)UIWebView *webView;
@property(nonatomic,strong)UIImageView *iconView;
@property(nonatomic)CGFloat height;

@end

@implementation QuestionContentView
{
    QuestionContentViewType type;
    NSIndexPath *indexPath;
    NSString *content;
    NSString *baseUrl;
}

-(instancetype)initWithFrame: (CGRect)frame
                    viewType: (QuestionContentViewType)viewType
                  htmlString: (NSString*)html
                     baseUrl: (NSString*)url
                 atIndexPath: (NSIndexPath*)index{
    
    if (self = [super initWithFrame:frame]) {
        type = viewType;
        indexPath = index;
        content = html;
        baseUrl = url;
        
        [self setupUI];
    }
    return self;
}

-(void)setHeight:(CGFloat)height{
    _height = height;
}

-(void)setDelegate:(id<QuestionContentViewDelegate>)delegate{
    _delegate = delegate;
    [_webView loadHTMLString:content baseURL:[NSURL fileURLWithPath:baseUrl]];
}

-(void)setupUI{
    _webView = [UIWebView new];
    _webView.scrollView.scrollEnabled = false;
    _webView.scrollView.contentInset = UIEdgeInsetsZero;
    _webView.delegate = self;
    _webView.backgroundColor = [UIColor clearColor];
    _webView.opaque = false;
    _webView.userInteractionEnabled = false;
    
    _iconView = [UIImageView new];
//    _iconView.backgroundColor = [UIColor blueColor];
    [self addSubview:_webView];
    [self addSubview:_iconView];
    
    // 约束
    CGFloat size = type == QuestionStem ? 0 : 23; //题干的话，就不显示icon
    [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(size == 0 ? 0 : 20));
        make.centerY.mas_equalTo(self.mas_centerY);
        make.width.height.equalTo(@(size));
    }];
    [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_iconView.mas_right).offset(10);
        make.right.equalTo(@(-20));
        make.centerY.mas_equalTo(self.mas_centerY);
        make.height.equalTo(@(0));
    }];
}

-(void)setIconSelected: (BOOL)selected{
    NSString *flag = type == 1 ? @"single" : @"multi"; //单选
    NSString *sel = selected ? @"sel" : @"normal";
    NSString *icon = [NSString stringWithFormat:@"radio_%@_%@",flag,sel];
    _iconView.image = [UIImage imageNamed:icon];
}

#pragma mark - webview delegate

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    return _height == 0;
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    CGFloat h = [[webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"content\").offsetHeight;"] floatValue];
    
    if (h > 0) {
        [_delegate questionContentView:self viewType:type contentDidLoadWithViewHeight:h indexPath:indexPath];
        _height = h;
        
        [self updateUI];
    }
}

-(void)updateUI{
    [_webView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(_height));
    }];
    [self setIconSelected:false];
}

@end
